package cate.game.role.pvp.arena;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import cate.game.role.farm.cache.AbstractCacheFarmFTC;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;

import java.util.List;

@NoteClass("跨服擂台赛FTC")
public class ArenaCacheFTC extends AbstractCacheFarmFTC<RoleArena> {

    public ArenaCacheFTC(Role role, byte posNum) {
        super(GDFunc.ARENA, role.getGame());
        super.posNum = posNum;
    }

    @Override
    protected RoleArena po(Role role) {
        return role.getPvp().arena;
    }

    @Override
    protected boolean winToNext(Role role, boolean win) {
        if (win) {
            // 当场赢的情况下,之前赢一场即可判定胜利
            return fightCache.getPlays(role.getUid()).stream()
                    .noneMatch(playPO -> playPO.winner == GDFight.Team.A);
        }
        // 当场输的情况下,只有一场未输才能继续
        return fightCache.getPlays(role.getUid()).stream()
                .noneMatch(playPO -> playPO.winner != GDFight.Team.A);
    }

    @Override
    protected boolean hasNext(Role role, PlayPO playPO) {
        return posNum < 3;
    }

    @Override
    protected EcResult<Void> challengeNext(Role role, byte nextPos) {
        return role.getPvp().arena.challenge(nextPos);
    }

    @Override
    protected void handleBefore(Role role, PlayPO playPO) {
        role.getPvp().arena.preWinner.add(playPO.winner);
    }

    @Override
    protected void handleFinish(Role role, PlayPO playPO, boolean win, boolean next, List<PlayPO> caches) {
        if (next) {
            return;
        }
        // 结算
        role.getPvp().arena.handleFinish(win, caches);
    }
}
